var prevCurrNode = null;
function orgQueue(){
	this.addNodes = [];
	this.updateNodes = [];
	this.delNodes = [];
};
function OrgNode(node){
	this.orgId = node.orgId;
	this.orgNo = node.orgNo;
	this.orgName = node.orgName;
	this.orgLevel = node.orgLevel;
	this.parOrgId = node.parOrgId;
	this.state = node.state;
}
var operQueue = new orgQueue();
function addQueue(node,type){
	switch(type){
	case 'add':
		operQueue.addNodes.push(new OrgNode(node));
		break;
	case 'del':
		operQueue.delNodes.push(new OrgNode(node));
		$.each(operQueue.updateNodes,function(index,item){
			if(item.orgId == node.orgId){
				operQueue.updateNodes.splice(index,1);
			}
		});
		break;
	case 'update':
		var isnew = true;
		$.each(operQueue.updateNodes,function(index,item){
			if(item.orgId == node.orgId){
				item.orgNo = node.orgNo;
				item.orgName = node.orgName;
				item.state = node.state;
				isnew = false;
				return;
			}
		});
		$.each(operQueue.addNodes,function(index,item){
			if(item.orgId == node.orgId){
				item.orgNo = node.orgNo;
				item.orgName = node.orgName;
				item.state = node.state;
				isnew = false;
				return;
			}
		});
		if(isnew){
			operQueue.updateNodes.push(new OrgNode(node));
		}
	}
}
function savePre(){
	if(prevCurrNode != null){  //保存前一个修改了的节点
		var orgNo = $.trim($("#nodesDetailFrm input[name='orgNo']").val());
		var orgName = $.trim($("#nodesDetailFrm input[name='orgName']").val());
		var state = $.trim($("#nodesDetailFrm select[name='state']").val());
		if(orgName == ''){
			orgsTree.selectNode(prevCurrNode,false);
			MsgBox.warn("机构名不能为空","错误提醒",null);
			return false;
		}
		if(orgNo == ""){
			orgNo = null;
		}
		if(prevCurrNode.orgNo != orgNo || prevCurrNode.orgName != orgName
				|| prevCurrNode.state != state){
			prevCurrNode.orgNo = orgNo;
			prevCurrNode.orgName = orgName;
			prevCurrNode.state = state;
			orgsTree.updateNode(prevCurrNode,true);
			addQueue(prevCurrNode,'update');
		}
	}
	return true;
}
var tree = {
	nodeClick:function(node){
		if(savePre()){
			$("#nodesDetailFrm").autofill(node);
			prevCurrNode = orgsTree.getCurrentNode();
		}
	}
}

var opreate = {
	fresh:function(){
		$("#nodesDetailFrm")[0].reset();
		queryOrgsAjax.send();
	},
	add:function(){
		var newNode = orgsTree.addNode(orgsTree.getCurrentNode());
		if(savePre()){
			$("#nodesDetailFrm").autofill(newNode);
			prevCurrNode = newNode;
			addQueue(newNode,'add');
		}
	},
	update:function(){
	},
	delNode:function(){
		var node = orgsTree.getCurrentNode();
		orgsTree.removeNode(node);
		$("#nodesDetailFrm")[0].reset();
		prevCurrNode = null;
		addQueue(node,'del');
	},
	save:function(){
		if(savePre()){
			commitChangesAjax.addParam("queue",$.toJSON(operQueue));
			commitChangesAjax.send();
		}
	},
	saveBefore:function(){
		$("#msg_dialog_loading").hide();
		$("#orgloading").show();
	},
	saveComplete:function(){
		$("#orgloading").hide();
		operQueue = new orgQueue();//清空队列
	},
	success:function(data){
		if(data.isSuccess == 'true' || data.isSuccess == true){
			MsgBox.info("保存成功！");
		}else{
			MsgBox.warn(data.message,"异常信息",function(){
				opreate.fresh();
			});
		}
	},
	error:function(XMLHttpRequest, textStatus, errorThrown){
		MsgBox.error("保存失败！","信息",function(){
			opreate.fresh();
		});
	}
}

var inputOper = {
		onblurFunc:function(){ 
			var orgName = $.trim($("#nodesDetailFrm input[name='orgName']").val());
			var node = orgsTree.getCurrentNode();
			if(orgName != node.orgName){
				node.orgName = orgName;
				orgsTree.updateNode(node,true);
				addQueue(node,'update');
			}
		}
}
var showTr = {
		closeloading:function(){
			$("#orgloading").hide();
			prevCurrNode = null;
			operQueue = new orgQueue();//清空队列
		},
		showloading:function(){
			$("#orgloading").show();
			$("#msg_dialog_loading").hide();
		}
}